extends Node
#["dog","oneMore","pairAdd","motivate","liveOrDie","dongSan","stove"]
# 定义方法表
var method_table = {
	"dog": dog,
	"oneMore": oneMore,
	"pairAdd": pairAdd,
	"motivate": motivate,
	"liveOrDie": liveOrDie,
	"dongSan": dongSan,
	"stove": stove,
	"versatile": versatile,
	"panda": panda,
	"half": half,
	"dice": dice,
	"blank1": blank1,
	"woodenMan": woodenMan,
	"dragon": dragon,
	"wind": wind,
	"luckyColor": luckyColor
}

# 根据 ID 调用方法
func call_method_by_id(functionCard):
	var id = functionCard.card_id
	if functionCard.card_id in method_table:
		await method_table[id].call(functionCard)
	else:
		print("找不到 ID:", id)


func call_method_by_add_id(functionCard):
	var id = functionCard.card_id
	var idArr = FunctionCardStrongLevel.addCardArr[id]
	for i in idArr:
		if i in method_table:
			await method_table[i].call(functionCard)
		else:
			print("找不到 ID:", i)

# 各种方法
func dog(functionCard):
	print("dog")

func oneMore(functionCard):
	print("oneMore")
	var root = get_tree().current_scene.get_children()[0]
	var playerFunctionCardArr = root.player_function_card_hbox.get_children()
	var verifyResult = root.verifyResult
	if verifyResult.pairDetailObj.pairCount != 0 or verifyResult.pongDetailObj.pongCount != 0 or verifyResult.kanDetailObj.kanCount != 0 or verifyResult.smallShunDetailObj.smallShunCount != 0 or verifyResult.bigShunDetailObj.bigShunCount != 0 or verifyResult.zfbDetailObj.zfbCount != 0 or verifyResult.fengDetailObj.fengCount != 0:
		for i in range(FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id]):
			#1.执行动画音乐效果
			await functionCard.functionCardShake(functionCard)
			functionCard.for_show_some_detail.text = "再来一次！"
			functionCard.for_show_some_detail.visible = true
			root.func_card_run_timer.start()
			await root.func_card_run_timer.timeout
			functionCard.for_show_some_detail.visible = false
			#2.执行实际逻辑
			root.gameProgress = 2
			await root.updateScoreByVerifyResult(verifyResult)
			root.gameProgress = 3
			#3.找到oneMore前的所有triggertiming为3的功能牌，并执行
			var index_of_onemore = -1
			for j in range(playerFunctionCardArr.size()):
				if playerFunctionCardArr[j].card_id == functionCard.card_id:
					index_of_onemore = j
					break
			if index_of_onemore != -1:
				var new_arr = playerFunctionCardArr.slice(0, index_of_onemore)
				if(new_arr.size() > 0):
					for m in new_arr:
						if m.trigger_timing == 3:
							await m.cardFunction()
			root.func_card_run_timer.start()
			await root.func_card_run_timer.timeout

func pairAdd(functionCard):
	print("pairAdd")
	var root = get_tree().current_scene.get_children()[0]
	var currentTypeString = root.currentTypeString
	if currentTypeString == "pair":
		#1.执行动画音乐效果
		await functionCard.functionCardShake(functionCard)
		#2.执行实际逻辑
		root.allScoreAdd += 15 * FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id]
		root.score_label.text = str(root.allScoreAdd)
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout

func motivate(functionCard):
	print("motivate")
	var root = get_tree().current_scene.get_children()[0]
	var playerFunctionCardArr = root.player_function_card_hbox.get_children()
	if functionCard.trigger_timing == 1:
		if playerFunctionCardArr.size() > 1:
			# 遍历数组，寻找"Motivate"节点的索引,或调用该方法的节点索引
			for i in range(playerFunctionCardArr.size()):
				if playerFunctionCardArr[i].name == functionCard.name:
					if i == 0:
						return
					else:
						var frontFunctionCard = playerFunctionCardArr[i - 1]
						var targetTriggerTiming = frontFunctionCard.trigger_timing
						functionCard.trigger_timing = targetTriggerTiming
						if frontFunctionCard.trigger_timing == 1:
							await functionCard.functionCardShake(functionCard)
							await frontFunctionCard.cardFunction()
	else:
		if playerFunctionCardArr.size() > 1:
			# 遍历数组，寻找"Motivate"节点的索引
			for i in range(playerFunctionCardArr.size()):
				if playerFunctionCardArr[i].name == functionCard.name:
					if i == 0:
						return
					else:
						await functionCard.functionCardShake(functionCard)
						for j in range(FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id]):
							var frontFunctionCard = playerFunctionCardArr[i - 1]
							await frontFunctionCard.cardFunction()

func liveOrDie(functionCard):
	print("liveOrDie")
	var root = get_tree().current_scene.get_children()[0]
	var playerFunctionCardArr = root.player_function_card_hbox.get_children()
	if functionCard.trigger_timing == 1:
		#1.执行动画音乐效果
		await functionCard.functionCardShake(functionCard)
		#2.执行抖动特效
		await functionCard.shakeTween(functionCard)
		await root.func_card_run_timer.timeout
		functionCard.for_show_some_detail.visible = false
		functionCard.trigger_timing = 3
		if randf() < 1.0 / 6.0:
			functionCard.for_show_some_detail.text = "亡"
			functionCard.for_show_some_detail.visible = true
			root.func_card_run_timer.start()
			await root.func_card_run_timer.timeout
			functionCard.for_show_some_detail.visible = false
			await functionCard.dieTween(functionCard)
			functionCard.queue_free()
			PlayerDetail.functionCardCarryArr.erase("liveOrDie")
		else:
			functionCard.for_show_some_detail.text = "存"
			functionCard.for_show_some_detail.visible = true
			root.func_card_run_timer.start()
			await root.func_card_run_timer.timeout
			functionCard.for_show_some_detail.visible = false
	else:
		await functionCard.functionCardShake(functionCard)
		root.allMagnification += (20 * FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id])
		root.magnification_label.text = str(root.allMagnification)
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout
	
func dongSan(functionCard):
	var root = get_tree().current_scene.get_children()[0]
	if RoundDetail.currentBigRound == 3 and RoundDetail.currentSmallRound == 1:
		#1.执行动画音乐效果
		await functionCard.functionCardShake(functionCard)
		#2.执行实际逻辑
		root.change_money.extraMoneyByFunctionCard += 50
		if FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id] > 1:
			root.change_money.extraMoneyByFunctionCard += (FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id] - 1) * 25
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout
	
func stove(functionCard):
	print("stove")
	var root = get_tree().current_scene.get_children()[0]
	var playerFunctionCardArr = root.player_function_card_hbox.get_children()
	var probability = 5
	if FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id] > 1:
		probability += (FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id] - 1)*5
	await functionCard.functionCardShake(functionCard)
	if randf() < probability / 100.0:
		#获取火炉的坐标
		var index_of_onemore = -1
		for j in range(playerFunctionCardArr.size()):
			if playerFunctionCardArr[j].card_id == functionCard.card_id:
				index_of_onemore = j
				break
		if index_of_onemore != -1:
			#火炉左Id
			if index_of_onemore-1 >= 0:
				var leftCardId = playerFunctionCardArr[index_of_onemore-1].card_id
				if leftCardId:
					await playerFunctionCardArr[index_of_onemore-1].shakeTween(playerFunctionCardArr[index_of_onemore-1])
					playerFunctionCardArr[index_of_onemore-1].for_show_some_detail.text = "强化成功"
					playerFunctionCardArr[index_of_onemore-1].for_show_some_detail.visible = true
					root.func_card_run_timer.start()
					await root.func_card_run_timer.timeout
					playerFunctionCardArr[index_of_onemore-1].for_show_some_detail.visible = false
					FunctionCardStrongLevel.strongLevelMapping[leftCardId] += 1
			#火炉右Id
			if index_of_onemore+1 <= playerFunctionCardArr.size()-1:
				var rightCardId = playerFunctionCardArr[index_of_onemore+1].card_id
				if rightCardId:
					await playerFunctionCardArr[index_of_onemore+1].shakeTween(playerFunctionCardArr[index_of_onemore+1])
					playerFunctionCardArr[index_of_onemore+1].for_show_some_detail.text = "强化成功"
					playerFunctionCardArr[index_of_onemore+1].for_show_some_detail.visible = true
					root.func_card_run_timer.start()
					await root.func_card_run_timer.timeout
					playerFunctionCardArr[index_of_onemore+1].for_show_some_detail.visible = false
					FunctionCardStrongLevel.strongLevelMapping[rightCardId] += 1
	else:
		#获取火炉的坐标
		var index_of_onemore = -1
		for j in range(playerFunctionCardArr.size()):
			if playerFunctionCardArr[j].card_id == functionCard.card_id:
				index_of_onemore = j
				break
		if index_of_onemore != -1:
			#火炉左Id
			if index_of_onemore-1 >= 0:
				var leftCardId = playerFunctionCardArr[index_of_onemore-1].card_id
				if leftCardId:
					await playerFunctionCardArr[index_of_onemore-1].shakeTween(playerFunctionCardArr[index_of_onemore-1])
					playerFunctionCardArr[index_of_onemore-1].for_show_some_detail.text = "强化失败"
					playerFunctionCardArr[index_of_onemore-1].for_show_some_detail.visible = true
					root.func_card_run_timer.start()
					await root.func_card_run_timer.timeout
					playerFunctionCardArr[index_of_onemore-1].for_show_some_detail.visible = false
			#火炉右Id
			if index_of_onemore+1 <= playerFunctionCardArr.size()-1:
				var rightCardId = playerFunctionCardArr[index_of_onemore+1].card_id
				if rightCardId:
					await playerFunctionCardArr[index_of_onemore+1].shakeTween(playerFunctionCardArr[index_of_onemore+1])
					playerFunctionCardArr[index_of_onemore+1].for_show_some_detail.text = "强化失败"
					playerFunctionCardArr[index_of_onemore+1].for_show_some_detail.visible = true
					root.func_card_run_timer.start()
					await root.func_card_run_timer.timeout
					playerFunctionCardArr[index_of_onemore+1].for_show_some_detail.visible = false
	root.func_card_run_timer.start()
	await root.func_card_run_timer.timeout

func versatile(functionCard):
	print("versatile")
	var root = get_tree().current_scene.get_children()[0]
	var playerFunctionCardArr = root.player_function_card_hbox.get_children()
	if functionCard.trigger_timing == 1:
		#百变，先改triggertiming为其左功能牌
		if playerFunctionCardArr.size() > 1:
			# 遍历数组，寻找"Versatile"节点的索引
			for i in range(playerFunctionCardArr.size()):
				if playerFunctionCardArr[i].name == functionCard.name:
					if i == 0:
						return
					else:
						#1.执行动画音乐效果
						await functionCard.functionCardShake(functionCard)
						#2.执行抖动特效
						await functionCard.shakeTween(functionCard)
						var frontFunctionCard = playerFunctionCardArr[i - 1]
						var targetTriggerTiming = frontFunctionCard.trigger_timing
						functionCard.trigger_timing = targetTriggerTiming
						var frontFuncCardImg = frontFunctionCard.func_card_img
						functionCard.func_card_img.texture = frontFuncCardImg.texture
						var frontFunctionCardId = frontFunctionCard.card_id
						if frontFunctionCard.trigger_timing == 1:
							if frontFunctionCardId in method_table:
								await method_table[frontFunctionCardId].call(functionCard)
							else:
								print("找不到 ID:", frontFunctionCardId)
		else:
			return
	else:
		if playerFunctionCardArr.size() > 1:
			# 遍历数组，寻找"Versatile"节点的索引
			for i in range(playerFunctionCardArr.size()):
				if playerFunctionCardArr[i].name == functionCard.name:
					if i == 0:
						return
					else:
						var frontFunctionCard = playerFunctionCardArr[i - 1]
						var frontFunctionCardId = frontFunctionCard.card_id
						if frontFunctionCardId in method_table:
							await method_table[frontFunctionCardId].call(functionCard)
						else:
							print("找不到 ID:", frontFunctionCardId)
		else:
			return

func panda(functionCard):
	print("panda")
	var root = get_tree().current_scene.get_children()[0]
	var tiaoArr = ["tiao_2","tiao_3","tiao_4","tiao_6","tiao_8"]
	var tiaoNum = 0
	for i in root.selectedTileIdArr:
		var splitArr = i.split("_")
		var tileType = splitArr[0] + "_" + splitArr[1]
		if tiaoArr.has(tileType):
			tiaoNum += 1
	if tiaoNum > 0:
		#1.执行动画音乐效果
		await functionCard.functionCardShake(functionCard)
		#2.执行实际逻辑
		root.allScoreAdd += (20 * FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id]) * tiaoNum
		root.score_label.text = str(root.allScoreAdd)
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout
		
func half(functionCard):
	print("half")
	var root = get_tree().current_scene.get_children()[0]
	if root.selectedTileIdArr.size() <= 3:
		await functionCard.functionCardShake(functionCard)
		root.allMagnification += (20 * FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id])
		root.magnification_label.text = str(root.allMagnification)
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout

func dice(functionCard):
	print("dice")
	await functionCard.shakeTween(functionCard)
	var root = get_tree().current_scene.get_children()[0]
	var result = randi() % 6 + 1  # 掷一个六面骰子，结果为 1-6
	functionCard.for_show_some_detail.text = "点数：" + str(result)
	functionCard.for_show_some_detail.visible = true
	root.func_card_run_timer.start()
	await root.func_card_run_timer.timeout
	functionCard.for_show_some_detail.visible = false
	if result > 3:
		await functionCard.functionCardShake(functionCard)
		root.allMagnification = 2 * (root.allMagnification) * FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id]
		root.magnification_label.text = str(root.allMagnification)
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout
	else:
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout

func blank1(functionCard):
	print("blank1")
	var currentIndex
	for i in range(PlayerDetail.functionCardCarryArr.size()):
		if PlayerDetail.functionCardCarryArr[i] == "blank1":
			currentIndex = i
	var root = get_tree().current_scene.get_children()[0]
	await functionCard.functionCardShake(functionCard)
	await functionCard.shakeTween(functionCard)
	# 定义要排除的值
	var excluded = ["blank1", "blank2", "blank3"]
	# 过滤数组，去掉排除项
	var filtered_cards = ShopDetail.functionCardArr.filter(func(item): return not excluded.has(item))
	# triggertiming = 1 随机取一个
	if functionCard.trigger_timing == 1:
		if filtered_cards.size() > 0:
			var random_card = filtered_cards[randi() % filtered_cards.size()]
			# 拼接功能牌场景路径
			var scenePath = "res://gameScene/functionCardScene/" + random_card + "FunctionCard/" + random_card + "FunctionCard.tscn"
			# 加载场景并确保返回的是一个 PackedScene
			var funcCardScene = load(scenePath)
			print("funcCardScene=",funcCardScene)
			# 检查是否是 PackedScene 类型
			if funcCardScene and funcCardScene is PackedScene:  
				# 实例化场景
				var funcCardInstance = funcCardScene.instantiate()
				if funcCardInstance:
					 # 将功能牌添加到 hbox 容器中
					root.player_function_card_hbox.remove_child(functionCard)
					root.player_function_card_hbox.add_child(funcCardInstance)
					root.player_function_card_hbox.move_child(funcCardInstance, currentIndex)
					if funcCardInstance.trigger_timing == 1:
						await funcCardInstance.cardFunction()
					functionCard.queue_free()
		else:
			print("没有可选卡牌（全部都是排除项）")
		
func woodenMan(functionCard):
	print("woodenMan")
	var root = get_tree().current_scene.get_children()[0]
	var currentTypeString = root.currentTypeString
	if currentTypeString == "smallShun":
		#1.执行动画音乐效果
		await functionCard.functionCardShake(functionCard)
		#2.执行实际逻辑
		root.allScoreAdd += 15 * FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id]
		root.score_label.text = str(root.allScoreAdd)
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout

func dragon(functionCard):
	print("dragon")
	var root = get_tree().current_scene.get_children()[0]
	var currentTypeString = root.currentTypeString
	if currentTypeString == "bigShun":
		#1.执行动画音乐效果
		await functionCard.functionCardShake(functionCard)
		#2.执行实际逻辑
		root.allScoreAdd += 100 * FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id]
		root.score_label.text = str(root.allScoreAdd)
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout

func wind(functionCard):
	print("wind")
	var root = get_tree().current_scene.get_children()[0]
	var currentTypeString = root.currentTypeString
	if currentTypeString == "feng":
		#1.执行动画音乐效果
		await functionCard.functionCardShake(functionCard)
		#2.执行实际逻辑
		root.allScoreAdd += 50 * FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id]
		root.score_label.text = str(root.allScoreAdd)
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout

func luckyColor(functionCard):
	print("luckyColor")
	var root = get_tree().current_scene.get_children()[0]
	var currentTypeString = root.currentTypeString
	if currentTypeString == "zfb":
		#1.执行动画音乐效果
		await functionCard.functionCardShake(functionCard)
		#2.执行实际逻辑
		root.allScoreAdd += 40 * FunctionCardStrongLevel.strongLevelMapping[functionCard.card_id]
		root.score_label.text = str(root.allScoreAdd)
		root.func_card_run_timer.start()
		await root.func_card_run_timer.timeout
